/**
 * Urban Parks
 *
 * Team M&M's:
 * Michael Satran Jr
 * Michael Pogson
 * Stephen Hunter
 *
 * Date: 2/16/2012
 */
package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * This is the panel which is displayed and signs a user into the system.
 * 
 * @author Michael Satran Jr
 */
public class SigninPanel extends UserPanel {
	
	/**
	 * A reference to the GUI so it can display a panel based on the user.
	 */
	private GUI my_gui;
	
	/**
	 * The frame which this window is contained.
	 */
	private JFrame my_frame;
	
	/**
	 * The panel which is the content pane.
	 */
	private JPanel my_panel;
	
	/**
	 * Email label, used to describe what the email text field is.
	 */
	private JLabel my_email_label;
	/**
	 * Email text field, where the user types their email.
	 */
	private JTextField my_email_text_field;
	/**
	 * Sign in button, what the user presses to sign in.
	 */
	private JButton my_submit_button;
	
	/**
	 * Creates a new SigninPanel.
	 * @param the_gui A reference to the GUI which displaying this.
	 */
	public SigninPanel(GUI the_gui) {
		my_gui = the_gui;
		my_panel = new JPanel();
		my_frame = new JFrame("Urban Parks - Sign-in");
		
		my_email_label = new JLabel("Email: ");
		my_email_text_field = new JTextField(20);
		my_submit_button = new JButton("Sign-in");
	}
	
	/**
	 * Signs a user in to the system and displays the proper panel.
	 */
	private void signIn() {
		try {
			//Set the current user in the controller
			my_gui.my_controller.setCurrentUser(my_email_text_field.getText());
			
			//Show the panel with the currently signed in user
			my_gui.showPanel(my_gui.my_controller.getCurrentUser());
			
			//Delete SignIn panel because it's worthless after signing in.
			my_frame.dispose();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(my_frame, e.getMessage());
		}
	}
	
	/**
	 * Sets up the sign in panel by adding components and such.
	 */
	public void setup() {
		
		my_email_text_field.addKeyListener(new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent arg0) {
				// TODO Auto-generated method stub
				if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
					signIn();
				}
			}
		});
		
		my_submit_button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent the_arg) {
				signIn();
			}
		});
		
		my_panel.add(my_email_label);
		my_panel.add(my_email_text_field);
		my_panel.add(my_submit_button);
	}
	
	/**
	 * Starts the sign in panel.
	 */
	public void start() {
		super.start(my_frame, my_panel);
	}

}
